package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.service.AdminConfigService;
import com.cskaoyan.market.db.admin.vo.BaseAdminConfigExpressVo;
import com.cskaoyan.market.db.admin.vo.BaseAdminConfigMallVo;
import com.cskaoyan.market.db.admin.vo.BaseAdminConfigOrderVo;
import com.cskaoyan.market.db.admin.vo.BaseAdminConfigWxVo;
import com.cskaoyan.market.db.domain.MarketSystem;
import com.cskaoyan.market.db.domain.MarketSystemExample;
import com.cskaoyan.market.db.mapper.MarketSystemMapper;
import com.cskaoyan.market.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 小焦
 * @since 2023/03/20 19:13
 */
@Service
public class AdminConfigServiceImpl implements AdminConfigService {
    @Override
    public BaseAdminConfigMallVo mall() {
        SqlSession sqlSession = MyBatisUtil.getSession();
        MarketSystemMapper systemMapper = sqlSession.getMapper(MarketSystemMapper.class);
        MarketSystemExample marketSystemExample = new MarketSystemExample();
        marketSystemExample.createCriteria().andKeyNameLike("market_mall_" + "%");
        List<MarketSystem> marketSystemList = systemMapper.selectByExample(marketSystemExample);
        sqlSession.commit();
        sqlSession.close();
        // BaseAdminConfigMallVo
        String className = "BaseAdminConfigMallVo";
        Object instance = null;
        try {
            instance = getConfigClass(marketSystemList, className);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        /*Map<String, MarketSystem> systemMap = new HashMap<>();
        for (MarketSystem marketSystem : marketSystemList) {
        	systemMap.put(marketSystem.getKeyName(),marketSystem);
        }
        // BaseAdminConfigMallVo configMallVo = new BaseAdminConfigMallVo();
        Class<? extends BaseAdminConfigMallVo> cls =BaseAdminConfigMallVo.class;
        try {
        	BaseAdminConfigMallVo configMallVo = cls.newInstance();
        Field[] declaredFields = cls.getDeclaredFields();
        for (Field field : declaredFields) {
        	// systemMap.get(field.getName())
        }
        } catch (InstantiationException e) {
        	e.printStackTrace();
        } catch (IllegalAccessException e) {
        	e.printStackTrace();
        }
        */
        return (BaseAdminConfigMallVo)instance;
    }

    @Override
    public Integer mallPost(BaseAdminConfigMallVo configMallVo) {
        SqlSession sqlSession = MyBatisUtil.getSession();
        MarketSystemMapper systemMapper = sqlSession.getMapper(MarketSystemMapper.class);
        Integer row = 0;
        try {
            row = updateSystemConfig(configMallVo, BaseAdminConfigMallVo.class, systemMapper);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        sqlSession.commit();
        sqlSession.close();
        return row;
    }

    @Override
    public BaseAdminConfigExpressVo express() {
        SqlSession sqlSession = MyBatisUtil.getSession();
        MarketSystemMapper systemMapper = sqlSession.getMapper(MarketSystemMapper.class);
        MarketSystemExample marketSystemExample = new MarketSystemExample();
        marketSystemExample.createCriteria().andKeyNameLike("market_express_freight_" + "%");
        List<MarketSystem> marketSystems = systemMapper.selectByExample(marketSystemExample);
        // 关闭SQLSession
        sqlSession.close();
        String className = "BaseAdminConfigExpressVo";
        Object instance = null;
        try {
            instance = getConfigClass(marketSystems, className);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return (BaseAdminConfigExpressVo)instance;
    }

    @Override
    public Integer expressPost(BaseAdminConfigExpressVo expressVo) {
        SqlSession sqlSession = MyBatisUtil.getSession();
        MarketSystemMapper systemMapper = sqlSession.getMapper(MarketSystemMapper.class);
        Integer row = 0;
        try {
            row = updateSystemConfig(expressVo, BaseAdminConfigExpressVo.class, systemMapper);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        sqlSession.commit();
        // 关闭SQLSession
        sqlSession.close();
        return row;
    }

    @Override
    public BaseAdminConfigOrderVo orderGet() {
        SqlSession sqlSession = MyBatisUtil.getSession();
        MarketSystemMapper systemMapper = sqlSession.getMapper(MarketSystemMapper.class);
        MarketSystemExample marketSystemExample = new MarketSystemExample();
        marketSystemExample.createCriteria().andKeyNameLike("market_order_" + "%");
        List<MarketSystem> marketSystems = systemMapper.selectByExample(marketSystemExample);
        sqlSession.close();
        String className="BaseAdminConfigOrderVo";
        Object instance = null;
        try {
            instance = getConfigClass(marketSystems, className);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return (BaseAdminConfigOrderVo) instance;
    }

    @Override
    public Integer orderPost(BaseAdminConfigOrderVo orderVo) {
        SqlSession sqlSession = MyBatisUtil.getSession();
        MarketSystemMapper systemMapper = sqlSession.getMapper(MarketSystemMapper.class);
        Integer row = 0;
        try {
            row = updateSystemConfig(orderVo, BaseAdminConfigOrderVo.class, systemMapper);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        sqlSession.commit();
        // 关闭SQLSession
        sqlSession.close();
        return row;
    }

    @Override
    public BaseAdminConfigWxVo wxGet() {
        SqlSession sqlSession = MyBatisUtil.getSession();
        MarketSystemMapper systemMapper = sqlSession.getMapper(MarketSystemMapper.class);
        MarketSystemExample marketSystemExample = new MarketSystemExample();
        marketSystemExample.createCriteria().andKeyNameLike("market_wx_" + "%");
        List<MarketSystem> marketSystems = systemMapper.selectByExample(marketSystemExample);
        sqlSession.close();
        String className="BaseAdminConfigWxVo";
        Object instance = null;
        try {
            instance = getConfigClass(marketSystems, className);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return (BaseAdminConfigWxVo) instance;
    }

    @Override
    public Integer wxPost(BaseAdminConfigWxVo wxVo) {
        SqlSession sqlSession = MyBatisUtil.getSession();
        MarketSystemMapper systemMapper = sqlSession.getMapper(MarketSystemMapper.class);
        Integer row = 0;
        try {
            row = updateSystemConfig(wxVo, BaseAdminConfigWxVo.class, systemMapper);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        sqlSession.commit();
        // 关闭SQLSession
        sqlSession.close();
        return row;
    }

    private <T> Integer updateSystemConfig(T target, Class aClass, MarketSystemMapper systemMapper)
        throws IllegalAccessException {
        int row = 0;
        Field[] declaredFields = aClass.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            declaredField.setAccessible(true);
            MarketSystem marketSystem = new MarketSystem();
            MarketSystemExample marketSystemExample = new MarketSystemExample();
            marketSystemExample.createCriteria().andKeyNameEqualTo(declaredField.getName());
            marketSystem.setKeyName(declaredField.getName());
            marketSystem.setKeyValue((String)declaredField.get(target));

            marketSystem.setUpdateTime(LocalDateTime.now());
            int i = systemMapper.updateByExampleSelective(marketSystem, marketSystemExample);
            row += i;
        }
        return row;
    }

    private Object getConfigClass(List<MarketSystem> marketSystemsList, String className)
        throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        Class<?> aClass = Class.forName(className);
        Object instance = aClass.newInstance();
        Field[] declaredFields = aClass.getDeclaredFields();
        for (MarketSystem marketSystem : marketSystemsList) {
            for (Field declaredField : declaredFields) {
                declaredField.setAccessible(true);
                if (declaredField.getName().equals(marketSystem.getKeyName())) {
                    declaredField.set(instance, marketSystem.getKeyValue());
                }
            }
        }
        return instance;
    }
}
